import { useUserStore } from "../store/user";

const whiteList = [
    '/pages/my/index',
    '/pages/hospital/list/index',
    '/pages/shop/list/index',
    '/pages/index/index',
    '/pages/wiki/detail/index',
    "/pages/wiki/list/index",
    "/pages/webview/doc",
	"/pages/reserve/index/index"
];

const isUserLoggedIn = () => {
    const userStore = useUserStore();
    return userStore.token && userStore.userInfo.id !== -1;
}

// 跳转到登录页面的统一方法
const redirectToLogin = () => {
    uni.showToast({
        title: '暂未登录',
        icon: 'none'
    })
    
    // 使用 switchTab 跳转到 tabBar 页面
    setTimeout(() => {
        uni.switchTab({
            url: '/pages/my/index'
        })
    }, 1000)
}

export const setupRouteGuard = () => {
    uni.addInterceptor('navigateTo', {
        invoke(args) {
            const isInWhiteList = whiteList.some(route => args.url.startsWith(route));
            
            if(isInWhiteList) {
                return args;
            }

            if(!isUserLoggedIn()) {
                redirectToLogin();
                // 抛出错误来阻止跳转
                throw new Error('未登录，阻止跳转');
            }

            return args;
        },
        fail(err) {
            // 静默处理我们主动抛出的错误
            if(err.message === '未登录，阻止跳转') {
                return;
            }
            // 其他错误正常处理
            console.error('navigateTo失败:', err);
        }
    })

    uni.addInterceptor('redirectTo', {
        invoke(args) {
            const isInWhiteList = whiteList.some(route => args.url.startsWith(route));
            
            if(isInWhiteList) {
                return args;
            }

            if(!isUserLoggedIn()) {
                redirectToLogin();
                throw new Error('未登录，阻止跳转');
            }

            return args;
        },
        fail(err) {
            if(err.message === '未登录，阻止跳转') {
                return;
            }
            console.error('redirectTo失败:', err);
        }
    })

    uni.addInterceptor('reLaunch', {
        invoke(args) {
            const isInWhiteList = whiteList.some(route => args.url.startsWith(route));
            
            if(isInWhiteList) {
                return args;
            }

            if(!isUserLoggedIn()) {
                redirectToLogin();
                throw new Error('未登录，阻止跳转');
            }

            return args;
        },
        fail(err) {
            if(err.message === '未登录，阻止跳转') {
                return;
            }
            console.error('reLaunch失败:', err);
        }
    })

    uni.addInterceptor('switchTab', {
        invoke(args) {
            const isInWhiteList = whiteList.some(route => args.url.startsWith(route));
            
            if(isInWhiteList) {
                return args;
            }

            if(!isUserLoggedIn()) {
                redirectToLogin();
                throw new Error('未登录，阻止跳转');
            }

            return args;
        },
        fail(err) {
            if(err.message === '未登录，阻止跳转') {
                return;
            }
            console.error('switchTab失败:', err);
        }
    })
}